<?php

/*

   <svn_trunk>/event_handlers/getMainJQGridDataEdit.php

   -- Handles the retrieval of data from the Annotations DB table
      in support of the main jqgrid in <svn_trunk>/editAnnotations.php

*/


require_once('../db_php/db_config.php');

if(isset($_GET['filenumber'])) 
{  $page = $_GET['page']; // get the requested page 
   $limit = $_GET['rows']; // get how many rows we want to have into the grid 
   $sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
   $sord = $_GET['sord']; // get the direction 
   $filenumber = $_GET['filenumber'];
   $annotator1 = $_GET['annotator1'];
} 
else  $filenumber = $annotator1 = $annotator2 = 1;

if(!$sidx) $sidx =1; $sord = "asc";

$result = mysql_query("SELECT COUNT(*) AS count 
                       FROM $dataTable
                       WHERE filenumber=$filenumber"); 
$row = mysql_fetch_array($result,MYSQL_ASSOC); 
$count = $row['count']; 

if($count >0 && $limit > 0)  $total_pages = ceil($count/$limit); 
else  $total_pages = 0; 

if($page > $total_pages)  $page=$total_pages; 

$start = $limit*$page - $limit; 

// do not put $limit*($page - 1) 

$dataSQL="SELECT d.postnum, timestamp, system, other, user, content, 
                 parenta, relationa, threada 
          FROM (SELECT * FROM $dataTable WHERE filenumber=$filenumber) d 
             LEFT JOIN 
                (SELECT a.filenumber,a.postnum, a.parent as parenta, 
                        a.relation as relationa, a.thread as threada
                 FROM $annotationsTable a 
                 WHERE a.filenumber = $filenumber 
                 AND a.annotator = $annotator1) a
             ON d.postnum=a.postnum;";

$data = mysql_query($dataSQL) 
    or die("Couldn't execute data query.".mysql_error()); 

$response->page = $page; $response->total = $total_pages; 
$response->userdata = array("fileno" => $filenumber, "filename" => $filename);
$response->records = $count; 

$order = array("postnum", "timestamp", "system", "user", "content", 
               "parenta", "relationa", "threada", 
               "parent", "relation", "thread");

$i=0;
while($row = mysql_fetch_array($data, MYSQL_ASSOC)) 
{  $response->rows[$i]['id']=$row[postnum];
   unset($row[id]);
   $response->rows[$i]['cell']= array_map(getVals, $order);
   $i++; 
} 
#print_r($response);
echo json_encode($response);

function getVals($v) {
  global $row;
  return $row[$v];
}
?>
